package src.leetcode.everyday.mid_3025;


import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: 李逸重
 * @CreateTime: 2025-09-04
 * @Description:
 * @Version: 1.0
 */
public class numberOfPairs {
    public static void main(String[] args) {
        System.out.println(new Solution().numberOfPairs(new int[][]{{0,3},{2,4},{0,6}}));
    }
}

class Solution {
    public int numberOfPairs(int[][] points) {
        Map<Integer, List<Integer>> listMap = new HashMap<>();
        for (int[] point : points) {
            List<Integer> list = listMap.getOrDefault(point[0], new ArrayList<>());
            list.add(point[1]);
            listMap.put(point[0], list);
        }

        List<List<Integer>> listList = listMap.entrySet().stream()
                .sorted(Map.Entry.comparingByKey()) // 按key排序
                .map(Map.Entry::getValue)           // 提取value
                .toList();
        int n = listList.size();
        int ans = 0;

        for (int i = 0; i < n; i++) {
            List<Integer> list = listList.get(i);
            ans += list.size() - 1;
            list.sort(Integer::compareTo);
        }

        for (int i = 0; i < n; i++) {
            List<Integer> list1 = listList.get(i);
            int m = list1.size();
            for (int l = 0; l < m; l++) {
                int[] max = new int[n];
                int cur = Integer.MIN_VALUE;
                if (l != 0) {
                    cur = list1.get(l - 1);
                }
                for (int k = i + 1; k < n; k++) {
                    List<Integer> list2 = listList.get(k);
                    int r = max[k];
                    if (list2.get(r) > list1.get(l)) {
                        continue;
                    }
                    while (r < list2.size() - 1 && list2.get(r + 1) <= list1.get(l)) {
                        r++;
                    }
                    if (list2.get(r) > cur) {
                        cur = list2.get(r);
                        ans++;
                    }
                    max[k] = r;
                }
            }
        }
        return ans;
    }
}
